﻿#region Globals.cs
// ============================================================================
// This region will be filled automatically with data if you are working
// for example with Microsoft SourceSafe
//
// Purpose:          
// Specifics: 
// 
// Creation date:    2024/1/3 21:31:07
// Created by:       hao.chen
//
// $Author:        $
// $Date:          $
// $Revision:      $
// $History:       $
// $NoKeywords:    $
// 
// Copyright:      © 2024 YourCompanyName
//                 All Rights Reserved
// ============================================================================
#endregion

using NeuroCheck.PlugInLib.Base;
using System;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;

// TODO: Namespace - Modify the namespace to the project namespace
namespace CLVisionHalconNC620
{
    /// <summary>Helper class designated as class with service functionality
    /// can be used by all check functions within this assembly.
    /// </summary>
    /// <remarks>Instead of AssemblyInstance.cs this class can be obfuscated.
    /// </remarks>
    public static class Globals
    {
        #region --- Event and delegate definitions ----------------------------
        #endregion ------------------------------------------------------------

        #region --- Private and public fields, except those wrapped by properties

        /// <summary>A NumberFormatInfo of current thread that defines the culturally appropriate
        /// format of displaying numbers, currency, and percentage.
        /// </summary>
        public static NumberFormatInfo m_nfiCurrentThread;
        /// <summary>Predefined xml attribute to read and write version information in xml files.
        /// </summary>
        public static string XmlArrtibuteVersion;

        #endregion ------------------------------------------------------------

        #region --- Constructors, including static constructors ---------------

        /// <summary>Constructor.
        /// </summary>
        static Globals()
        {
            // Please be aware if you modify this assignments
            m_nfiCurrentThread = Thread.CurrentThread.CurrentCulture.NumberFormat;
            XmlArrtibuteVersion = "version";
        }

        #endregion ------------------------------------------------------------

        #region --- Instance public properties (and the private fields they wrap)
        #endregion ------------------------------------------------------------

        #region --- Instance public methods -----------------------------------
        #endregion ------------------------------------------------------------

        #region --- Static public methods and properties ----------------------

        /// <summary>Returns a version from the given string in format 1.2.3.4.
        /// </summary>
        /// <param name="stringVersion">The version as string.</param>
        /// <returns>Version or null on invalid string format.</returns>
        /// <exception cref="NcPIUserException">An error occurred.</exception>
        public static Version GetVersionFromString(string stringVersion)
        {
            // Version that will be returned
            Version version = null;
            string message;

            try
            {
                // Split the version number in values
                string[] splittedVersion = stringVersion.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                // Verify the version consists of 4 values
                if (4 == splittedVersion.Length)
                {
                    try
                    {
                        // Extract the version number values
                        int major = Convert.ToInt32(splittedVersion[0]);
                        int minor = Convert.ToInt32(splittedVersion[1]);
                        int build = Convert.ToInt32(splittedVersion[2]);
                        int revision = Convert.ToInt32(splittedVersion[3]);

                        // Create the Version object
                        version = new Version(major, minor, build, revision);

                    } // eof try
                    catch
                    {
                        message = String.Format(Properties.GlobalResources.strWrongVersionNumber, stringVersion);
                        NcPIUserException newEx = new NcPIUserException(message);
                        throw newEx;

                    } // eof catch
                } // eof if
                else
                {
                    message = String.Format(Properties.GlobalResources.strWrongVersionNumber, stringVersion);
                    NcPIUserException newEx = new NcPIUserException(message);
                    throw newEx;

                } // eof else
            } // eof try
            catch (NcPIUserException ex)
            {
                throw ex;

            } // eof catch
            catch (Exception ex)
            {
                NcPIUserException newEx = new NcPIUserException(ex.Message);
                throw newEx;

            } // eof catch

            return version;
        }

        /// <summary>Verifies if the passed point exceeds the working area of the primary screen
        /// and if so the Point(0, 0) will be returned.
        /// </summary>
        /// <param name="point">Point to verify.</param>
        /// <returns>If the passed point exceeds the working area the Point (0, 0) will be returned; otherwise the passed point.</returns>
        public static Point GetUsableDialogLocation(Point point)
        {
            // Check if the passed point exceeds the working area of the primary screen
            if (true == Screen.PrimaryScreen.WorkingArea.Contains(point))
            {
                return point;

            } // eof if
            else
            {
                return new Point(0, 0);

            } // eof else
        }

        /// <summary>Converts the given string to double.
        /// </summary>
        /// <param name="valueAsString">String to convert.</param>
        /// <param name="nfi">NumberFormatInfo for the convertion method.</param>
        /// <param name="valueAsDouble">The converted string as double.</param>
        /// <returns>True if the conversion was successful; otherwise false.</returns>
        public static bool ConvertToDouble(string valueAsString, NumberFormatInfo nfi, out double valueAsDouble)
        {
            valueAsDouble = 0.0;

            // Check the string
            if (true == String.IsNullOrEmpty(valueAsString))
            {
                return false;

            } // eof if

            // Convert the value to double
            return Double.TryParse(valueAsString, NumberStyles.Any, nfi, out valueAsDouble);
        }

        #endregion ------------------------------------------------------------

        #region --- Methods in interfaces -------------------------------------
        #endregion ------------------------------------------------------------

        #region --- Private (helper) methods ----------------------------------
        #endregion ------------------------------------------------------------

    } // end of class
} // end of namespace